package com.windoent.yugioh.adapter.banner;

import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.windoent.yugioh.R;
import com.windoent.yugioh.help.glide.GlideRoundTransform;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * author : robert
 * date   : 2021/5/6 15:12
 * desc   :
 */
public class ImageAdapter extends BannerAdapter<String, ImageAdapter.BannerViewHolder> {


    public ImageAdapter(List<String> mDatas) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
    }

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        ImageView imageView = new ImageView(parent.getContext());
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return new BannerViewHolder(imageView);
    }

    @Override
    public void onBindView(BannerViewHolder holder, String data, int position, int size) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .error(R.drawable.drawable_no_data) //加载失败图片
                .priority(Priority.HIGH) //优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存
                .transform(new GlideRoundTransform(8)); //圆角
        if (holder.itemView.getContext() != null) {
            Glide.with(holder.itemView.getContext().getApplicationContext())
                    .load(data)
                    .apply(options)
                    .into(holder.imageView);
        }
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public BannerViewHolder(@NonNull ImageView view) {
            super(view);
            this.imageView = view;
        }
    }
}
